package com.zch.mobile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.base.dialect.PaginationSupport;
import com.base.mobile.MobException;
import com.base.mobile.MobileInfo;
import com.base.mobile.server.annotation.Mobile;
import com.base.mobile.server.annotation.MobileMethod;
import com.base.util.DateUtil;
import com.bf.common.proto.MProjectMsgList.ProjectMsgDetail;
import com.bf.common.proto.MProjectMsgList.ProjectMsgDetailList;
import com.bf.common.proto.MUserCenter.MMyFindMsg;
import com.bf.common.proto.MUserCenter.MMyFindMsgList;
import com.bf.common.proto.MUserCenter.MMyPublish;
import com.bf.common.proto.MUserCenter.MMyPublishList;
import com.bf.common.proto.MZhiku;
import com.item.service.UserService;
import com.zch.dao.model.Ability;
import com.zch.dao.model.AbilityExample;
import com.zch.dao.model.Activitymessage;
import com.zch.dao.model.ActivitymessageExample;
import com.zch.dao.model.ApplyProject;
import com.zch.dao.model.Bank;
import com.zch.dao.model.BankExample;
import com.zch.dao.model.Collegemessage;
import com.zch.dao.model.CollegemessageExample;
import com.zch.dao.model.Policy;
import com.zch.dao.model.PolicyExample;
import com.zch.dao.model.Principal;
import com.zch.dao.model.PrincipalExample;
import com.zch.dao.model.ProjectMessage;
import com.zch.dao.model.ServiceOrganization;
import com.zch.dao.model.ServiceOrganizationExample;
import com.zch.dao.model.Technologymessage;
import com.zch.dao.model.TechnologymessageExample;
import com.zch.dao.model.Xqmessage;
import com.zch.dao.model.XqmessageExample;
import com.zch.dao.model.Zaiti;
import com.zch.dao.model.ZaitiExample;
import com.zch.dao.model.ZxMessage;
import com.zch.dao.model.ZxMessageExample;
import com.zch.daoEx.model.ActivitymessageEx;
import com.zch.daoEx.model.ApplyProjectEx;
import com.zch.daoEx.model.ServiceOrganizationEx;
import com.zch.daoEx.model.ZhikuEx;
import com.zch.daoEx.model.ZhikuTypeEx;
import com.zch.service.AbilityService;
import com.zch.service.ActivitymessageService;
import com.zch.service.ApplyProjectService;
import com.zch.service.BankService;
import com.zch.service.CollegemessageService;
import com.zch.service.PolicyService;
import com.zch.service.PrincipalService;
import com.zch.service.ProjectMessageService;
import com.zch.service.ServiceOrganizationService;
import com.zch.service.TechnologymessageService;
import com.zch.service.XqmessageService;
import com.zch.service.ZaitiService;
import com.zch.service.ZhikuService;
import com.zch.service.ZhikuTypeService;
import com.zch.service.ZxMessageService;

@Mobile
@Component
public class MAppUserInfo {

	@Autowired
	private ZxMessageService zxMessageService;
	@Autowired
	private ZaitiService zaitiService;
	@Autowired
	private XqmessageService xqmessageService;
	@Autowired
	private PrincipalService principalService;
	@Autowired
	private ActivitymessageService activitymessageService;
	@Autowired
    private ProjectMessageService projectMessageService;
	@Autowired
    private UserService userService;
	@Autowired
	private ZhikuTypeService zhikuTypeService;
	@Autowired
	private ZhikuService zhikuService;
	@Autowired
    private PolicyService policyService;
    @Autowired
    private TechnologymessageService technologymessageService;
    @Autowired
    private ApplyProjectService applyProjectService;
    @Autowired
    private ServiceOrganizationService serviceOrganizationService;
    @Autowired
    private CollegemessageService collegemessageService;
    @Autowired
    private AbilityService abilityService;
    @Autowired
    private BankService bankService;
    
	/**
	@api MMyPublishList MMyPublishList //我的发布(登陆,分页)
	@param required number type	//1资讯2技术3载体4资本5活动6政策
	@return MMyPublishList
	*/
	@MobileMethod(methodno = "MMyPublishList", isLogin = true, isPage = true)
	public MMyPublishList.Builder myPublistList(MobileInfo mobileInfo, Integer type, Integer page, Integer limit) throws Exception {
		if (type == null) throw new MobException("type");
		MMyPublishList.Builder ret = MMyPublishList.newBuilder();
		PaginationSupport.byPage(page, limit, false);
		switch (type) {
			case 1:
				ZxMessageExample messageExample = new ZxMessageExample();
				messageExample.createCriteria().andUserIdEqualTo(mobileInfo.getUserid());
				messageExample.setOrderByClause("create_time desc");
				List<ZxMessage> zxMessages = zxMessageService.selectByExample(messageExample);
				for (ZxMessage message : zxMessages){
					MMyPublish.Builder mini = MMyPublish.newBuilder();
					mini.setId(message.getId());
					mini.setImg(message.getImage());
					mini.setState(message.getIsAgree());
					mini.setTitle(message.getZxTitle());
					mini.setType(type);
					mini.setFType(message.getType()+"");
					if(message.getType()!=null){
					    mini.setCtype(message.getType().toString());
					    mini.setUrl(message.getContent());
					}
					/*if (message.getType() == 1){
						mini.setUrl(message.getContent());
					}*/
					if (message.getIsAgree() == 2){
						mini.setReason(message.getNotAgreeReason());
					}
					mini.setTime(DateUtil.dateToStr(message.getCreateTime(), DateUtil.YYMMMM));
					ret.addList(mini);
				}
				break;
			case 2:
				XqmessageExample xqmessageExample = new XqmessageExample();
				xqmessageExample.setOrderByClause("create_time desc");
				xqmessageExample.createCriteria().andUserIdEqualTo(mobileInfo.getUserid());
				List<Xqmessage> xqmessages = xqmessageService.selectByExample(xqmessageExample);
				for (Xqmessage message : xqmessages){
					MMyPublish.Builder mini = MMyPublish.newBuilder();
					mini.setId(message.getId());
					mini.setState(message.getIsAgree());
					mini.setTitle(message.getName());
					mini.setType(1);
					mini.setTime(DateUtil.dateToStr(message.getCreateTime(), DateUtil.YYMMMM));
					if (message.getIsAgree() == 2){
						mini.setReason(message.getNotAgreeReason());
					}
					ret.addList(mini);
				}
				
				TechnologymessageExample technologymessageExample = new TechnologymessageExample();
				technologymessageExample.setOrderByClause("create_time desc");
				technologymessageExample.createCriteria().andUserIdEqualTo(mobileInfo.getUserid());
				List<Technologymessage> lists = technologymessageService.selectByExample(technologymessageExample);
				for(Technologymessage message : lists){
				    MMyPublish.Builder mini = MMyPublish.newBuilder();
				    mini.setId(message.getId());
				    mini.setState(message.getIsAgree());
				    mini.setTitle(message.getTechnologyName());
				    mini.setType(2);
				    mini.setTime(DateUtil.dateToStr(message.getCreateTime(), DateUtil.YYMMMM));
				    if(message.getIsAgree()==2){
				        mini.setReason(message.getNotAgreeReason());
				    }
				    ret.addList(mini);
				}
				
				CollegemessageExample collegemessageExample = new CollegemessageExample();
				collegemessageExample.setOrderByClause("create_time desc");
				collegemessageExample.createCriteria().andUserIdEqualTo(mobileInfo.getUserid());
				List<Collegemessage> collegemessageList = collegemessageService.selectByExample(collegemessageExample);
				for(Collegemessage message : collegemessageList){
                    MMyPublish.Builder mini = MMyPublish.newBuilder();
                    mini.setId(message.getId());
                    mini.setState(message.getIsAgree());
                    mini.setTitle(message.getName());
                    mini.setType(3);
                    mini.setTime(DateUtil.dateToStr(message.getCreateTime(), DateUtil.YYMMMM));
                    if(message.getIsAgree()==2){
                        mini.setReason(message.getNotAgreeReason());
                    }
                    ret.addList(mini);
                }
				
				AbilityExample abilityExample = new AbilityExample();
				abilityExample.setOrderByClause("create_time desc");
				abilityExample.createCriteria().andUserIdEqualTo(mobileInfo.getUserid());
				List<Ability> abilityList = abilityService.selectByExample(abilityExample);
				for(Ability message : abilityList){
                    MMyPublish.Builder mini = MMyPublish.newBuilder();
                    mini.setId(message.getId());
                    mini.setState(message.getIsAgree());
                    mini.setTitle(message.getTitle());
                    mini.setType(4);
                    mini.setTime(DateUtil.dateToStr(message.getCreateTime(), DateUtil.YYMMMM));
                    if(message.getIsAgree()==2){
                        mini.setReason(message.getNotAgreeReason());
                    }
                    ret.addList(mini);
                }
				
				break;
			case 3:
				ZaitiExample zaitiExample = new ZaitiExample();
				zaitiExample.setOrderByClause("create_time desc");
				zaitiExample.createCriteria().andUserIdEqualTo(mobileInfo.getUserid());
				List<Zaiti> zaitis = zaitiService.selectByExample(zaitiExample);
				for (Zaiti message : zaitis){
					MMyPublish.Builder mini = MMyPublish.newBuilder();
					mini.setId(message.getId());
					mini.setState(message.getIsAgree());
					mini.setTitle(message.getZaitiName());
					mini.setType(type);
					mini.setImg(message.getImage());
					mini.setTime(DateUtil.dateToStr(message.getCreateTime(), DateUtil.YYMMMM));
					if (message.getIsAgree() == 2){
						mini.setReason(message.getNotAgreeReason());
					}
					ret.addList(mini);
				}
				break;
			case 4:
				PrincipalExample example = new PrincipalExample();
				example.setOrderByClause("create_time desc");
				example.createCriteria().andUserIdEqualTo(mobileInfo.getUserid());
				List<Principal> list = principalService.selectByExample(example);
				for(Principal ex : list){
				    MMyPublish.Builder mini = MMyPublish.newBuilder();
				    mini.setId(ex.getId());
				    mini.setState(ex.getIsVerify());
				    mini.setTitle(ex.getOrganizationInfo());
				    mini.setImg(ex.getImgs());
				    mini.setType(1);
				    mini.setTime(DateUtil.dateToStr(ex.getCreateTime(), DateUtil.YYMMMM));
				    if (ex.getIsVerify()==2){
				        mini.setReason(ex.getNotAgreeReason());
				    }
				    ret.addList(mini);
				}
				
				BankExample bankExample = new BankExample();
				bankExample.setOrderByClause("create_time desc");
				bankExample.createCriteria().andUserIdEqualTo(mobileInfo.getUserid());
                List<Bank> bankList = bankService.selectByExample(bankExample);
                for(Bank ex : bankList){
                    MMyPublish.Builder mini = MMyPublish.newBuilder();
                    mini.setId(ex.getId());
                    mini.setState(ex.getIsAgree());
                    mini.setTitle(ex.getBankName());
                    mini.setType(2);
                    mini.setTime(DateUtil.dateToStr(ex.getCreateTime(), DateUtil.YYMMMM));
                    if (ex.getIsAgree()==2){
                        mini.setReason(ex.getNotAgreeReason());
                    }
                    ret.addList(mini);
                }
				break;
			case 5:
				ActivitymessageExample activitymessageExample = new ActivitymessageExample();
				activitymessageExample.setOrderByClause("create_time desc");
				activitymessageExample.createCriteria().andUserIdEqualTo(mobileInfo.getUserid());
				List<Activitymessage> activitymessages = activitymessageService.selectByExample(activitymessageExample);
				for (Activitymessage message : activitymessages){
					MMyPublish.Builder mini = MMyPublish.newBuilder();
					mini.setId(message.getId());
					mini.setState(message.getIsAgree());
					mini.setTitle(message.getActivityName());
					mini.setType(type);
					mini.setFType(message.getType()+"");
					mini.setImg(message.getImage());
					mini.setTime(DateUtil.dateToStr(message.getCreateTime(), DateUtil.YYMMMM));
					if(message.getType()!=null){
                        mini.setCtype(message.getType().toString());
                        mini.setUrl(message.getContent());
                    }
					if (message.getIsAgree() == 2){
						mini.setReason(message.getNotAgreeReason());
					}
					ret.addList(mini);
				}
				break;
			case 6:
			    PolicyExample policyExample = new PolicyExample();
			    policyExample.setOrderByClause("create_time desc");
			    policyExample.createCriteria().andUserIdEqualTo(mobileInfo.getUserid());
                List<Policy> policys = policyService.selectByExample(policyExample);
                for (Policy message : policys){
                    MMyPublish.Builder mini = MMyPublish.newBuilder();
                    mini.setId(message.getId());
                    mini.setState(message.getIsAgree());
                    mini.setTitle(message.getTitle());
                    mini.setType(type);
                    mini.setImg(message.getImage());
                    mini.setTime(DateUtil.dateToStr(message.getCreateTime(), DateUtil.YYMMMM));
                    if(message.getType()!=null){
                        mini.setCtype(message.getType().toString());
                        mini.setUrl(message.getContent());
                    }
                    if (message.getIsAgree() == 2){
                        mini.setReason(message.getNotAgreeReason());
                    }
                    ret.addList(mini);
                }
                break;
			default:
				break;
		}
		return ret;
	}
	
	/**
	 * 我的--我的发布项目列表
	 * @param mobileInfo
	 * @param page
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MMyProjectList", isLogin = true, isPage = true)
	public ProjectMsgDetailList.Builder MyProjectMsgList(MobileInfo mobileInfo, Integer page, Integer limit )throws Exception{
	    PaginationSupport.byPage(page, limit, false);
	    Map<String, Object> map = new HashMap<String, Object>();
	    String user_id = mobileInfo.getUserid();
	    
	    
	    map.put("user_id", user_id);
	    List<ProjectMessage> projectMessageList = projectMessageService.selectMyProject(map);
	    ProjectMsgDetailList.Builder ret = ProjectMsgDetailList.newBuilder();
	    for(ProjectMessage ex : projectMessageList){
	        ProjectMsgDetail.Builder c = ProjectMsgDetail.newBuilder();
	        c.setId(ex.getId());
	        c.setTitle(ex.getTitle());
	        c.setCreateTime(DateUtil.dateToStr(ex.getCreateTime()));
	        c.setProjectState(ex.getProjectState().toString());
	        c.setProjectShutdownReason(ex.getProjectShutdownReason());
	        c.setIsEnable(ex.getIsEnable().toString());
	        c.setIsAgree(ex.getIsAgree().toString());
	        c.setNotAgreeReason(ex.getNotAgreeReason());
	        c.setType("0"); //无按钮
	        if(user_id == ex.getAnalyzeMaster()){
	            c.setType("1"); //项目可行性分析 
	        }
	        if(user_id == ex.getConnectMaster()){
	            c.setType("1"); //项目对接或跟踪汇报
	        }
	        if(user_id == ex.getRegisterMaster()){
	            c.setType("1"); //项目公司注册
	        }
	        if(user_id == ex.getFollowMaster()){
	            c.setType("1"); // 运营跟踪
	        }
	        ret.addProjectMsgDetail(c);
	    }
	    return ret;
	    
	}
	
	/**
	 * 发现列表
	 * @param mobileInfo
	 * @param type
	 * @param page
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MMyFindMsgList", isLogin = false)
	public MMyFindMsgList.Builder MMyFindMsgList(MobileInfo mobileInfo)throws Exception{
	    MMyFindMsgList.Builder ret = MMyFindMsgList.newBuilder();
	    
	    int limit = 3;
	    
	    // 申报
	    Map<String, Object> shenbaoMap = new HashMap<String, Object>();
	    shenbaoMap.put("limit", limit);
	    List<ApplyProjectEx> applyProjectList = applyProjectService.selectFindMsgList(shenbaoMap);
	    for(ApplyProjectEx ex : applyProjectList){
	        MMyFindMsg.Builder b = MMyFindMsg.newBuilder();
	        b.setId(ex.getId());
	        b.setTitle(ex.getTitle());
	        b.setCreateTime(DateUtil.dateToStr(ex.getCreateTime()));
	        ret.addShenbao(b);
	    }
	    
	    // 活动
        /*Map<String, Object> activityMap = new HashMap<String, Object>();
        activityMap.put("limit", limit);
        List<ActivitymessageEx> activityList = activitymessageService.selectAppList(activityMap);
        for(ActivitymessageEx activity : activityList)
        {
            MMyFindMsg.Builder b = MMyFindMsg.newBuilder();
            b.setId(activity.getId());
            b.setTitle(activity.getActivityName());
            b.setCreateTime(DateUtil.dateToStr(activity.getCreateTime()));
            ret.addActivity(b);
        }*/
	    
	    // 智库
	    Map<String, Object> zhikuMap = new HashMap<String, Object>();
		zhikuMap.put("limit", limit);
		zhikuMap.put("isAgree", 1);
		List<ZhikuEx> zhikuList = zhikuService.selectList(zhikuMap);
		for (ZhikuEx zhiku : zhikuList)
		{
			MMyFindMsg.Builder b = MMyFindMsg.newBuilder();
			b.setId(zhiku.getId());
			b.setTitle(zhiku.getName());
			b.setCreateTime(DateUtil.dateToStr(zhiku.getCreateTime()));
			ret.addZhiku(b);
		}
		
		//服务机构
		Map<String, Object> serOr = new HashMap<String, Object>();
		serOr.put("limit", limit);
		serOr.put("isAgree", 1);
        List<ServiceOrganizationEx> list = serviceOrganizationService.selectFindMsgList(serOr);
        for (ServiceOrganization ex : list)
        {
            MMyFindMsg.Builder b = MMyFindMsg.newBuilder();
            b.setId(ex.getId());
            b.setTitle(ex.getTitle());
            b.setCreateTime(DateUtil.dateToStr(ex.getCreateTime()));
            ret.addActivity(b);
        }
	    
    	return ret;
	}
	
	/**
	 * 获取智库类型列表
	 * @param mobileInfo
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MZhikuTypeList", isLogin = false)
	public MZhiku.ZhikuTypeList.Builder ZhikuTypeList(MobileInfo mobileInfo) throws Exception
	{
		Map<String, Object> map = new HashMap<String, Object>();
		List<ZhikuTypeEx> list = zhikuTypeService.selectList(map);
		
		MZhiku.ZhikuTypeList.Builder ret = MZhiku.ZhikuTypeList.newBuilder();
		
		// 找出大类
		List<ZhikuTypeEx> mainTypes = findZhikuTypes(list, "0");
		for (ZhikuTypeEx mainType: mainTypes)
		{
			MZhiku.ZhikuType.Builder mainBuilder = MZhiku.ZhikuType.newBuilder();
			mainBuilder.setId(mainType.getId());
			mainBuilder.setName(mainType.getName());
			
			/*// 找出小类
			List<ZhikuTypeEx> childTypes = findZhikuTypes(list, mainType.getId());
			for (ZhikuTypeEx childType : childTypes)
			{
				MZhiku.ZhikuType.Builder childBuilder = MZhiku.ZhikuType.newBuilder();
				childBuilder.setId(childType.getId());
				childBuilder.setName(childType.getName());
				mainBuilder.addChildType(childBuilder);
			}*/
			
			ret.addType(mainBuilder);
		}
		
		return ret;
	}
	
	/**
	 * 找出指定parentId的智库类型
	 * @param list
	 * @param parentId
	 * @return
	 */
	private static List<ZhikuTypeEx> findZhikuTypes(List<ZhikuTypeEx> types, String parentId)
	{
		List<ZhikuTypeEx> results = new ArrayList<ZhikuTypeEx>();
		for (ZhikuTypeEx type : types)
		{
			if (type.getParentId().equals(parentId))
			{
				results.add(type);
			}
		}
		return results;
	}
	
	
	/**
	 * 智库列表
	 * @param mobileInfo
	 * @param page
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MZhikuList", isLogin = false, isPage = true)
	public MZhiku.ZhikuList.Builder zhikuList(MobileInfo mobileInfo, Integer page, Integer limit, String zhikuTypeId) throws Exception 
	{
		PaginationSupport.byPage(page, limit, false);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isAgree", 1);
		if (!StringUtils.isBlank(zhikuTypeId))
		{
			map.put("zhikuTypeId", zhikuTypeId);
		}
		List<ZhikuEx> list = zhikuService.selectList(map);

		MZhiku.ZhikuList.Builder ret = MZhiku.ZhikuList.newBuilder();
		for (ZhikuEx ex : list) 
		{
			MZhiku.ZhikuListItem.Builder c = MZhiku.ZhikuListItem.newBuilder();
			c.setId(ex.getId());
			c.setName(ex.getName());
			c.setCreateTime(DateUtil.dateToStr(ex.getCreateTime()));
			ret.addItem(c);
		}
		return ret;
	}
	
	/**
	 * 获取智库详情
	 * @param mobileInfo
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MZhikuDetail", isLogin = false)
	public MZhiku.ZhikuDetail.Builder zhikuDetail(MobileInfo mobileInfo, String id) throws Exception 
	{
		if (StringUtils.isBlank(id))
		{
			throw new MobException("id");	
		}
		
		ZhikuEx ex = zhikuService.selectDetailById(id);
		if (ex == null)
		{
            throw new MobException(MZchError.CONTENT_NOT_EXIST);
        }
		
		MZhiku.ZhikuDetail.Builder c = MZhiku.ZhikuDetail.newBuilder();
		c.setId(ex.getId());
		c.setName(ex.getName());
		c.setZhikuTypeName(ex.getZhikuTypeName());
		c.setCreateTime(DateUtil.dateToStr(ex.getCreateTime()));
		c.setContent(ex.getContent());
		c.setIsAgree(ex.getIsAgree().toString());

		return c;
	}
}
